package com.androidme.app.framework.base;

import android.content.Context;
import android.os.Environment;
import com.androidme.app.framework.cache.AbstractCache;
import com.androidme.app.framework.http.BetterHttp;
import roboguice.application.RoboApplication;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

//@ReportsCrashes(formKey = "",
//        formUri = "http://t.corp.snda.com/st/upload.php",
//        mode = ReportingInteractionMode.TOAST,
//        resToastText = R.string.crash_toast_text)
public class AndroidMeApplication extends RoboApplication {
    public static final String XMLNS = "http://code.google.com/p/android-me/schema";
    private static final int CORE_POOL_SIZE = 5;
    private static final String APP_NAME = "androidme";
    private static final String APP_VERSION = "0.1";
    public static final File FILE_LOCAL = new File(Environment.getExternalStorageDirectory(), APP_NAME);
    public static final File FILE_PIC_ADVERT = new File(FILE_LOCAL, "images/adv");

    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "AndroidMe thread #" + mCount.getAndIncrement());
        }
    };

    private final int backgroundMusic = 0;
    private ExecutorService coreThreadPool;
    private ExecutorService commonThreadPool;

    @Override
    public void onCreate() {
        super.onCreate();
//        ACRA.init(this);
        BetterHttp.setupHttpClient();
        BetterHttp.enableGZIPEncoding();
        BetterHttp.enableResponseCache(getApplicationContext(), 10, 60, 1, AbstractCache.DISK_CACHE_SDCARD);
    }


    public int getBackgroundMusic() {
        return backgroundMusic;
    }

    private HashMap<String, WeakReference<Context>> contextObjects = new HashMap<String, WeakReference<Context>>();

    public synchronized Context getActiveContext(String className) {
        WeakReference<Context> ref = contextObjects.get(className);
        if (ref == null) {
            return null;
        }

        final Context c = ref.get();
        if (c == null) // If the WeakReference is no longer valid, ensure it is removed.
            contextObjects.remove(className);

        return c;
    }

    public synchronized boolean containActiveContext(String className) {
        WeakReference<Context> ref = contextObjects.get(className);
        if (ref == null) {
            return false;
        }

        final Context c = ref.get();
        if (c == null)
            return false;
        return true;
    }

    public synchronized void setActiveContext(String className, Context context) {
        WeakReference<Context> ref = new WeakReference<Context>(context);
        this.contextObjects.put(className, ref);
    }

    public synchronized void resetActiveContext(String className) {
        contextObjects.remove(className);
    }

    public ExecutorService getCoreThreadPool() {
        if (coreThreadPool == null) {
            coreThreadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE, sThreadFactory);
        }
        return coreThreadPool;
    }

    public ExecutorService getCommonThreadPool() {
        if (commonThreadPool == null) {
            commonThreadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE, sThreadFactory);
        }
        return commonThreadPool;
    }
}
